//依附于SysMenu上的登录Panel，包括登录，登出

var SysMenuLoginHandler = function(text, setcookie){
	if (text == null){
		messageBox.Show("登录：连接服务器失败! 请检查网络设置", sysMenuScene.layer);
		menuPanel.SetAllManipulateEnable(true);
		return;
	}
	if (text == "" || text.length < 5){
		messageBox.Show("登录失败! 用户名/密码 错误", sysMenuScene.layer);
		menuPanel.SetAllManipulateEnable(true);
		return;
	}
	menuPanel.SetAllManipulateEnable(true);
	var user = JSON.parse(text);
	if (user == null || user.user == null || user.user.id == null || user.id < 0){
		messageBox.Show("登录失败! 服务器异常", sysMenuScene.layer);
		return;
	}
	menuPanel.SetAllManipulateEnable(false);
	userSet = user;
	//校正客户端时间
	RalibrationDatetime(userSet.time);
	menuPanel.HideMenu();
	//no bad result of layer-destroy
	setTimeout(function(){ menuPanel.ShowMenu(2,0);}, (menuPanel.scrollTime + 0.1) * 1000);
};

var SysMenuGetUserHandler = function(text, setcookie){
	menuPanel.SetAllManipulateEnable(true);
	
	if (text == null)
		messageBox.Show("获取用户：连接服务器失败! 请检查网络设置", sysMenuScene.layer);
	else{
		cc.log("text:" + text);
	}
};

var SysMenuLogoutHandler = function(text, setcookie){
	menuPanel.SetAllManipulateEnable(true);
	
	if (text == null){
		menuPanel.SetAllManipulateEnable(true);
		messageBox.Show("登出：连接服务器失败! 请检查网络设置", sysMenuScene.layer);
		return;
	}
	if (text != "true"){
		menuPanel.SetAllManipulateEnable(true);
		messageBox.Show("登出失败! 系统异常", sysMenuScene.layer);
		return;
	}
	userSet = { user:null, gameUser:gameUserEngine.CreateGameUser() };
	menuPanel.HideMenu();
	//no bad result of layer-destroy
	setTimeout(	function(){ menuPanel.ShowMenu(1,0);}, (menuPanel.scrollTime + 0.1) * 1000);
};

var menuPanel = {
	menuStatus : 0,//0:hidden  1:login-mode  2:menu-mode
	outmenuNode : null,
	menuNode : null,
	scrollY : null,
	scrollTime : 1.0,
	userNameBox : null,
	passwordBox : null,
	
	loginMenu:null,
	registerMenu:null,
	localMenu:null,
	exitMenu:null,
	welcomeMenu:null,
	startNewMenu:null,
	battlenetMenu:null,
	settingMenu:null,
	exitMenu:null,
	
	//bug: MenuItemLabel cannot set attribute 'enabled = false';
	welcomeMenuEnable:true,
	
	Initialize : function(){
		this.menuStatus = 0;
	},
	
	ShowMenu : function(status, delayTime){
		if (menuPanel.menuStatus == status)
			return;

		this.menuStatus = status;
		
		this.loginMenu=null;
		this.registerMenu=null;
		this.localMenu=null;
		this.exitMenu=null;
		this.welcomeMenu=null;
		this.startNewMenu=null;
		this.battlenetMenu=null;
		this.settingMenu=null;
		this.exitMenu=null;
		
		this.welcomeMenuEnable=true;
		
		var winSize = cc.director.getWinSize();

		this.outmenuNode = new cc.Node();
		sysMenuScene.layer.addChild(this.outmenuNode, 3);
		
		var menubackground = new cc.Scale9Sprite(resource.scale9halfblack100100_png, cc.rect(0,0,100,100), cc.rect(8,8,84,84));
		menubackground.setContentSize(820, 500);
		
		this.scrollY = menubackground.height;
		this.menuNode = new cc.Node();
		this.menuNode.attr({
			x: winSize.width / 2,
			y: sysMenuScene.layer.logo.y - sysMenuScene.layer.logo.height * 0.5 + this.scrollY * 0.6
		});
		sysMenuScene.layer.clippedmenu.addChild(this.menuNode);
		
		this.menuNode.addChild(menubackground);
		
		if (status == 1){
			//add labels and textBoxes
			var userNameLabel = cc.LabelTTF.create("用户名", "simhei", 46);
			this.menuNode.addChild(userNameLabel);
			
			// Create the textfield
			var scale9sprite = new cc.Scale9Sprite(resource.scale9white100100_png, cc.rect(0,0,100,100), cc.rect(8,8,84,84));
			this.userNameBox = new cc.EditBox(cc.size(menubackground.width * 0.5, menubackground.height * 0.15), scale9sprite);
			this.userNameBox.setFontColor(cc.color(0, 0, 0));
			this.userNameBox.setFont("simhei", 46);
			this.userNameBox.setString("FirstUser");
			//this.userNameBox.setString("USER/EMAIL/PHONE");
			this.userNameBox.setDelegate(sysMenuScene.layer);
			this.menuNode.addChild(this.userNameBox);
			
			//add labels and textBoxes
			var passwordLabel = cc.LabelTTF.create("密  码", "simhei", 46);
			this.menuNode.addChild(passwordLabel);

			// Create the textfield
			var scale9sprite = new cc.Scale9Sprite(resource.scale9white100100_png, cc.rect(0,0,100,100), cc.rect(8,8,84,84));
			this.passwordBox = new cc.EditBox(cc.size(menubackground.width * 0.5, menubackground.height * 0.15), scale9sprite);
			this.passwordBox.setFontColor(cc.color(0, 0, 0));
			this.passwordBox.setFont("simhei", 46);
			this.passwordBox.setString("871023");
//			this.passwordBox.setString("PASSWORD");
			this.passwordBox.setInputFlag(cc.EDITBOX_INPUT_FLAG_PASSWORD);
			this.passwordBox.setDelegate(sysMenuScene.layer);
			this.menuNode.addChild(this.passwordBox);
			
			// Add Menus
			this.loginMenu = this._CreateShortButton("登录",0,0,this._OnLogin);
			this.registerMenu = this._CreateShortButton("注册",0,0,this._OnRegister);
			this.localMenu = this._CreateShortButton("单机",0,0,this._OnLocal);
			this.exitMenu = this._CreateShortButton("退出",0,0,this._OnGameExit);
			
			var menu = cc.Menu.create(this.loginMenu, this.registerMenu, this.localMenu, this.exitMenu);
			menu.attr({ x: 0, y: 0 });
			this.menuNode.addChild(menu);

			ArrangeVertical(0, -menubackground.height * 0.1, menubackground.height * 0.08, [this.userNameBox, this.passwordBox, this.loginMenu]);
			ArrangeHorizontal(0, this.userNameBox.y, menubackground.width * 0.06, [userNameLabel, this.userNameBox]);
			ArrangeHorizontal(0, this.passwordBox.y, menubackground.width * 0.06, [passwordLabel, this.passwordBox]);
			ArrangeHorizontal(0, this.loginMenu.y, menubackground.width * 0.03, [this.loginMenu, this.registerMenu, this.localMenu, this.exitMenu]);

		}else if (status == 2){
			//add menu Items
			var wordXScale = 1.5;
			this.startNewMenu = this._AddMenu("开始新战役", 0, 0, this._OnStartCampaign);
			this.battlenetMenu = this._AddMenu("连接战网", 0, 0, this._OnBattleNet);
			this.settingMenu = this._AddMenu("设置", 0, 0, this._OnGameSetting);
			this.exitMenu = this._AddMenu("退出游戏", 0, 0, this._OnGameExit);
			ArrangeVertical(0, menubackground.height * (- 0.08), menubackground.height * (0.08), [this.startNewMenu, this.battlenetMenu, this.settingMenu, this.exitMenu ]);
			
			var menu = cc.Menu.create(this.startNewMenu, this.battlenetMenu, this.settingMenu, this.exitMenu);
			menu.attr({
				x: ((winSize.width - this.menuNode.x) * wordXScale)
				- (winSize.width - this.menuNode.x),
				y: 0,
				scaleX : wordXScale
			});
			this.menuNode.addChild(menu);
			
			var hours = (new Date()).getHours();
			var timeText = "晚上";
			if (hours > 5 && hours < 12)
				timeText = "早上";
			else if (hours >= 12 && hours < 18)
				timeText = "下午";
			var welcomeText = timeText + "好! " + userSet.gameUser.name + "[" + rankName[userSet.gameUser.rank - 1] + "] (点击注销)";
			var welcomeLabel = cc.LabelTTF.create(welcomeText, "simhei", 26);
			welcomeLabel.setColor(cc.color(255,64,0));
			this.welcomeMenu = new cc.MenuItemLabel(welcomeLabel,
				function callback(sender){
					menuPanel._OnLogout(sender);
				},
				sysMenuScene.layer);
			this.welcomeMenu.attr({anchorX:1.0, anchorY:1.0, x:winSize.width - 18, y:winSize.height - 12});
			var outmenu = cc.Menu.create( this.welcomeMenu );
			outmenu.attr({ x:0, y:0 });
			
			this.outmenuNode.addChild(outmenu);
		}

		//actions
		this.menuNode.runAction(cc.sequence(
				cc.delayTime(delayTime),
				cc.moveBy( this.scrollTime, 0, -(this.scrollY) )));
	},
	
	HideMenu : function(){
		if (this.menuStatus == 0)
			return;
		this.menuStatus = 0;

		if (this.outmenuNode != null){
			this.outmenuNode.removeFromParent();
			this.outmenuNode = null;
		}
		if (this.menuNode != null){
			this.menuNode.runAction(cc.sequence(
					cc.moveBy(this.scrollTime, 0, this.scrollY),
					cc.removeSelf()));
		}
	},
	
	SetAllManipulateEnable : function(enable){
		if (this.loginMenu!=null){ this.loginMenu.enabled = enable; }
		if (this.registerMenu!=null){ this.registerMenu.enabled = enable; }
		if (this.localMenu!=null){ this.localMenu.enabled = enable; }
		if (this.exitMenu!=null){ this.exitMenu.enabled = enable; }
		if (this.welcomeMenu!=null){ this.welcomeMenu.enabled = enable; }
		welcomeMenuEnable = enable;
		if (this.startNewMenu!=null){ this.startNewMenu.enabled = enable; }
		if (this.battlenetMenu!=null){ this.battlenetMenu.enabled = enable; }
		if (this.settingMenu!=null){ this.settingMenu.enabled = enable; }
		if (this.exitMenu!=null){ this.exitMenu.enabled = enable; }
	},
	
	_CreateShortButton : function(text,xpos,ypos,callback){
		var button = new cc.MenuItemImage(resource.ctrlbtnNormalS_png,resource.ctrlbtnDownS_png, resource.ctrlbtnDisableS_png, callback, this);
		button.setPosition(xpos, ypos);
		button.setScale(1.35);
		var label = cc.LabelTTF.create(text, "simhei", 48);
		label.setPosition(button.width * 0.5, button.height * 0.5);
		label.setColor(cc.color(0,0,0));
		label.setScale(0.5);
		button.addChild(label);
		return button;
	},
	
	_AddMenu : function(text, xpos, ypos, callbackfunction) {
		var label = cc.LabelTTF.create(text, "simhei", 46);
		var menuItem = new cc.MenuItemLabel(label, function callback(sender){
			cc.audioEngine.playEffect(resource.click_wav);
			sender.setColor(cc.color(255,64,0));
			//no bad result of layer-destroy
			setTimeout(function(){
				sender.setColor(cc.color(255,255,255));
				callbackfunction(sender);
			}, 0.25 * 1000);
		}, sysMenuScene.layer);

		menuItem.attr({anchorX: 0.5, x:xpos, y:ypos});
		return menuItem;
	},
	
	_OnLogin: function(sender) {
		var userName = this.userNameBox.getString();
		var password = this.passwordBox.getString();
		if (userName != "USERNAME" && userName != "" && password != "PASSWORD" && password !=""){
			netService.Login(userName, password, SysMenuLoginHandler, 3);
			menuPanel.SetAllManipulateEnable(false);
			messageBox.Show("正在登录...", sysMenuScene.layer);
		}else{
			messageBox.Show("用户名/密码不合法!", sysMenuScene.layer);
		}
	},
	
	_OnLogout: function(sender) {
		if (userSet.user == null){
			menuPanel.HideMenu();
			//no bad result of layer-destroy
			setTimeout(function(){ menuPanel.ShowMenu(1,0);}, (menuPanel.scrollTime + 0.1) * 1000);
		}else{
			menuPanel.SetAllManipulateEnable(false);
			netService.Logout(userSet.user.id, SysMenuLogoutHandler, 3);
			messageBox.Show("正在注销...", sysMenuScene.layer);
		}
	},
	
	_OnRegister: function(sender) {
		if (!cc.sys.isNative) {
			cc.window.open("http://www.baidu.com");
		}else{
			messageBox.Show("注册请到网站: http://www.baidu.com", sysMenuScene.layer);
		}
	},

	_OnLocal: function(sender) {
		userSet = { user:null, gameUser:gameUserEngine.CreateGameUser() };
		this.HideMenu();
		//no bad result of layer-destroy
		setTimeout(function(){ menuPanel.ShowMenu(2,0);}, (this.scrollTime + 0.1) * 1000);
	},

	_OnStartCampaign: function(sender) {
		gameRoom = gameRoomEngine.CreateGameRoom(campaign,
				[userSet.gameUser, userSet.gameUser],
				[-1, -1]);
		cc.LoaderScene.preload(campaignScene.resource, function() {
			cc.director.runScene(campaignScene.Create());
		}, sysMenuScene.layer);
	},

	_OnBattleNet: function(sender) {
		var background = new cc.Sprite(resource.background_png);
		background.attr({scale:SC, color:cc.color(192,192,192)});
		var stageSheet = new StageSheetLayer(false, false, background);
		sysMenuScene.scene.addChild(stageSheet);
		stageSheet.show();
	},

	_OnGameSetting: function(sender) {
		var background = new cc.Sprite(resource.background_png);
		background.attr({scale:SC, color:cc.color(192,192,192)});
		var setting = new SettingLayer(false, true, background);
		sysMenuScene.scene.addChild(setting);
		setting.show();
	},

	_OnGameExit: function(sender) {
		cc.director.popScene();
	}
};